<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
webUtils.nameSpace("wiseMen.platform");
wiseMen.platform.optAuthGrid=function(cfg){
	cfg=cfg||{};
	var me=this;
	me.grid=cfg.gridDom.bootstrapTable({
		url:ctx + "/optAuthAction/queryInFuncPoint.do",
		queryParams:function(d){
			if(me.grid){
    			d.funcPointId=me.grid.funcPointId;
    		}
			
			return d;
		},
    	columns:[
			{field: '_id',checkbox:true},
            {title: '操作名称', field: 'name' , width: "120px"},
            {title: '编码', field: 'code' , width: "300px"}
    	]
    });
	
	if(cfg.toolbarSelector){
		var toolbar=$(cfg.toolbarSelector);
		toolbar.find(".batAddBt").click(function(){
			if (!me.grid.funcPointId) {
                webUtils.alert("请选择具体的功能点");
            }else{
            	var sub = top.webUtils.topicSubscribe("optAuth.edit.success", function (args) {
            		dialog.close();
                    me.grid.bootstrapTable("refresh");
                });
            	
            	var dialog=webUtils.dialog({
                  	 title:'批量操作权限',
                  	 url:ctx + '/optAuthAction/addByBatchView.do?funcPointId='+ me.grid.funcPointId,
                  	 onremove:function () {
                  		sub.remove();
                       }
                   });
            }
		});
		
		toolbar.find(".addBt").click(function(){
			if (!me.grid.funcPointId) {
                webUtils.alert("请选择具体的功能点");
                return false;
            }
			var sub = top.webUtils.topicSubscribe("optAuth.edit.success", function (args) {
            	dialog.close();
                me.grid.bootstrapTable("refresh");
            });
            var dialog = editOptAuth({
                funcPointId: me.grid.funcPointId,
                onClose: function () {
                    sub.remove();
                }
            });
		});
		
		toolbar.find(".editBt").click(function(){
			var rows=me.grid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var sub = top.webUtils.topicSubscribe("optAuth.edit.success", function (args) {
    				dialog.close();
                    me.grid.bootstrapTable("refresh");
                });
        		var row=rows[0];
        		var dialog = editOptAuth({
                    id: row.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
        	}else {
                webUtils.alert("请选择需要编辑的数据");
            }
		});
		
		toolbar.find(".delBt").click(function(){
			var rows=me.grid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var selecteds = [];
		                $.each(rows, function (i, n) {
		                    selecteds.push(n.id);
		                });
		                $.post(ctx + "/optAuthAction/delete.do", {
		                	ids: selecteds.join(",")
		                }, function (data) {
		                    if (data.success) {
		                    	me.grid.bootstrapTable("refresh");
		                    } else {
		                    	webUtils.alert(data.msg);
		                    }
		                }, 'json');
					}
        		});
        		
        	}else {
        		webUtils.alert("请选择需要删除的数据");
            }
		});
		
		toolbar.find(".roleAuthBt").click(function(){
			var rows=me.grid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var row = rows[0];
        		$.getJSON(ctx+"/optAuthAction/loadRoles.do",{
            		id:row.id
            	},function(data){
            		data=data||{};
            		webUtils.roleSelector({
            			hasSelectedRoles:data,
                		onSelect:function(rows){
                			var roles=[];
                			$.each(rows,function(i,n){
                				roles.push(n.id);
                			});
                				$.post(ctx + "/optAuthAction/setRoles.do", {
                                    id:row.id,roleIds: roles.join(",")
                               }, function (data) {
                                   if (data.success) {
                                   		webUtils.alert("配置成功"); 
                                   } else {
                                	   webUtils.error(data.msg);
                                   }
                               }, 'json');
                		}
            		});
            	});
        	}else {
                webUtils.alert("请选择需要授权的数据");
            }
		});
	}
	
	
	var editOptAuth = function (params) {
        params = params || {};
        var t = [];
        if (params.id) {
            t.push("id=" + params.id);
        }
        if (params.funcPointId) {
            t.push("funcPointId=" + params.funcPointId);
        }
        
        var dialog=webUtils.dialog({
       	 title:'编辑操作权限',
       	 url:ctx + '/optAuthAction/editView.do?' + t.join("&"),
       	 onremove:function () {
                if (params.onClose) {
                    params.onClose.call(null);
                }
            }
        });
        return dialog;
    }
	
	return me.grid;
}
</script>